package hz.mobileproject.pda.utils;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;

import hz.mobileproject.pda.entity.request.Request_data;
import hz.mobileproject.pda.listener.CustomNetCallBack;

/**
 * Created by Lc on 2017/12/8.
 */

public class HttpRqUtils {

    public static void httpPost(String url , Object o , CustomNetCallBack callback){
        Gson gson = new Gson();
        String fpStr = gson.toJson(o);
//        LogUtils.e(fpStr);
        Request_data data = new Request_data();
        data.setData(Base64.getBase64(fpStr));
        String body_data = gson.toJson(data);

        RequestParams params = new RequestParams(url);
        params.setBodyContent(body_data);
        params.setHeader("token", SPUtils.getInstance("app_login").getString("token"));
        x.http().post(params, callback);
    }

    public static void httpPost(String url , Object[] objects , CustomNetCallBack callback){
        Gson gson = new Gson();
        String fpStr = gson.toJson(objects);
        Request_data data = new Request_data();
        data.setData(Base64.getBase64(fpStr));
        String body_data = gson.toJson(data);

        RequestParams params = new RequestParams(url);
        params.setBodyContent(body_data);
        params.setHeader("token", SPUtils.getInstance("app_login").getString("token"));
        x.http().post(params, callback);
    }

//    public static void httpPost(String url ,List objects , CustomNetCallBack callback){
//        Gson gson = new Gson();
//        String fpStr = gson.toJson(objects);
////        LogUtils.e(fpStr);
//        Request_data data = new Request_data();
//        data.setData(Base64.getBase64(fpStr));
//        String body_data = gson.toJson(data);
//
//        RequestParams params = new RequestParams(url);
//        params.setBodyContent(body_data);
//        x.http().post(params, callback);
//    }

    public static void httpPost(String url , Object object , File f , CustomNetCallBack callback){
        Gson gson = new Gson();
        String fpStr = gson.toJson(object);
//        LogUtils.e(fpStr);
        Request_data data = new Request_data();
        data.setData(Base64.getBase64(fpStr));
        String body_data = gson.toJson(data);

        RequestParams params = new RequestParams(url);
        if(f == null){
            params.addBodyParameter("data", body_data);
        }else {
            params.addBodyParameter("data", body_data,"application/octet-stream");
            params.addBodyParameter("file",f,"application/octet-stream");
        }
        params.setHeader("token", SPUtils.getInstance("app_login").getString("token"));
        x.http().post(params, callback);
    }


    public static void httpPost(String url , Object object , File[] files , CustomNetCallBack callback){
        Gson gson = new Gson();
        String fpStr = gson.toJson(object);
//        LogUtils.e(fpStr);
        Request_data data = new Request_data();
        data.setData(Base64.getBase64(fpStr));
        String body_data = gson.toJson(data);

        RequestParams params = new RequestParams(url);
        if(files == null){
            params.addBodyParameter("data", body_data);
        }else {
            params.addBodyParameter("data", body_data,"application/octet-stream");
            for(File f : files){
                params.addBodyParameter("file",f,"application/octet-stream");
            }
        }
        params.setHeader("token", SPUtils.getInstance("app_login").getString("token"));
        x.http().post(params, callback);
    }


//    public static void httpPost(String url , String json , File f , CustomNetCallBack callback){
//        Gson gson = new Gson();
//        LogUtils.e(json);
//        Request_data data = new Request_data();
//        data.setData(Base64.getBase64(json));
//        String body_data = gson.toJson(data);
//        RequestParams params = new RequestParams(url);
//        if (null != f){
//            params.addBodyParameter("data", body_data);
//            params.addBodyParameter("file",f,"application/octet-stream");
//        }else {
//            params.addBodyParameter("data", body_data,"application/octet-stream");
//        }
//        x.http().post(params, callback);
//    }

    public static void httpGet(String url , CustomNetCallBack callback){
        RequestParams params = new RequestParams(url);
        params.setHeader("token", SPUtils.getInstance("app_login").getString("token"));
        x.http().get(params, callback);
    }

    public static void httpGet(String url , Callback.CommonCallback<String> callback){
        RequestParams params = new RequestParams(url);
        params.setHeader("token", SPUtils.getInstance("app_login").getString("token"));
        x.http().get(params, callback);
    }
}
